package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.ThirdPartyCompanyDTO;
import com.financing.platform.service.IProjectCompanyService;
import com.financing.platform.service.IThirdPartyCompanyService;
import com.financing.platform.util.DTOdateSet;

/**
 * 第三方公司相关
 * @author cuimo
 *
 */
@Controller("thirdPartyProjectCompanyActionAPI")
@RequestMapping("/api/3rdProjectCompany")
@Scope("prototype")
public class ThirdPartyCompanyAction extends BaseAction {

	@Autowired
	private IThirdPartyCompanyService tpCompanyService;

	@RequestMapping("/addTPC.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request,
			HttpServletResponse response, ThirdPartyCompanyDTO tpCompanyDTO) {
		try {
			log.info("In add3rdProjectCompany add begin!");

			// 初始化数据赋值
			DTOdateSet.setCreateInfo(tpCompanyDTO, request.getSession());
			DTOdateSet.setUpdateInfo(tpCompanyDTO, request.getSession());
			// 拼音缩写变大写
			if(null != tpCompanyDTO.getTpcAbbrLetter()){
				tpCompanyDTO.setTpcAbbrLetter(tpCompanyDTO.getTpcAbbrLetter().toUpperCase());
			}
			tpCompanyService.addThirdPartyCompany(tpCompanyDTO);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("add3rdProjectCompany add error !", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setMessage(e.getCause().getMessage());
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		log.info("In add3rdProjectCompany add end!");
		return resultMap;
	}
	/**
	* 
	* @Title: update 
	* @Description: 修改产品公司
	* @param   request
	* @param   response
	* @param   projectCompanyDTO
	* @return ResultMap   
	* @throws
	 */
	@RequestMapping("/updateTPC.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request,
			HttpServletResponse response,ThirdPartyCompanyDTO tpCompanyDTO,Pager pager) {
		
		log.info("In update3rdProjectCompany update begin!");
		
		try{
			if(tpCompanyDTO.getId() == null){
				resultMap.setStatus(STATUS.FAILE);
				log.info("In ProjectCompanyAction deleteCompany projectCompanyDTO id is null!");
				return resultMap;
			}
			DTOdateSet.setUpdateInfo(tpCompanyDTO, request.getSession());
			// 拼音缩写变大写
			if(null != tpCompanyDTO.getTpcAbbrLetter()){
				tpCompanyDTO.setTpcAbbrLetter(tpCompanyDTO.getTpcAbbrLetter().toUpperCase());
			}
			tpCompanyService.updateThirdPartyCompany(tpCompanyDTO);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("update3rdProjectCompany error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In update3rdProjectCompany end!");	
		
		return resultMap;
	}
	/**
	* 
	* @Title: 查询公司列表
	* @param   request
	* @param   response
	* @param   projectCompanyDTO
	* @return ResultMap   
	* @throws
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/queryTPC.shtml")
	@ResponseBody
	public ResultMap query(HttpServletRequest request,
			HttpServletResponse response,ThirdPartyCompanyDTO tpCompanyDTO,Pager pager) {
		
		log.info("In query3rdProjectCompany begin!");
		
		try{
			
			tpCompanyDTO.setBegin(pager.getStartIndex());
			tpCompanyDTO.setPageSize(pager.getPageSize());
			// 判断关键字是字母还是汉字
			if(null != tpCompanyDTO.getKeyWord() && tpCompanyDTO.getKeyWord().matches("^[a-zA-Z]*")){
				tpCompanyDTO.setTpcAbbrLetter(tpCompanyDTO.getKeyWord().toUpperCase());
			}else if(null != tpCompanyDTO.getKeyWord()){
				tpCompanyDTO.setTpcFullName(tpCompanyDTO.getKeyWord());
				tpCompanyDTO.setTpcShortName(tpCompanyDTO.getKeyWord());
			}
			
			int count = tpCompanyService.getThirdPartyCompanyListCount(tpCompanyDTO);
			
			pager.setTotalCount(count);
			
			List<ThirdPartyCompanyDTO> list;
			if(count>0){
				list = tpCompanyService.getThirdPartyCompanyList(tpCompanyDTO);
			}else{
				list = Collections.EMPTY_LIST;
			}
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("list",list);
			resultMap.setAttribute("pager",pager);
		}catch(Exception e){
			log.error("query3rdProjectCompany error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In query3rdProjectCompany end!");	
		
		return resultMap;
	}
	/**
	* 
	* @Title:  deleteTPC
	* @Description: 删除三方
	* @param   request
	* @param   response
	* @param   advertId
	* @return ResultMap   
	* @throws
	 */
	@RequestMapping("/deleteTPC.shtml")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request,
			HttpServletResponse response,Integer tpCompanyId) {
		
		log.info("In delete3rdProjectCompany Begin!");
		
		try{
			if(tpCompanyId == null){
				resultMap.setStatus(STATUS.FAILE);
				log.info("In delete3rdProjectCompany tpCompanyId is null!");
				return resultMap;
			}
			tpCompanyService.delThirdPartyCompany(tpCompanyId);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("delete3rdProjectCompany error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In delete3rdProjectCompany end!");	
		
		return resultMap;
	}
}
